//
//  Fraction.m
//  02_ClassAndObject
//
//  Created by 徐昊 on 16/7/5.
//  Copyright © 2016年 徐昊. All rights reserved.
//

#import "Fraction.h"

@implementation Fraction

- (instancetype)initWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator {
    self = [super init];
    if (self) {
        _numerator = numerator;
        _denominator = denominator;
    }
    return self;
}

+ (instancetype)frationWithNumerator:(NSInteger)numerator denominator:(NSInteger)denominator {
    
    return [[Fraction alloc] initWithNumerator:numerator denominator:denominator];
    
}

- (Fraction *)plusWithFraction:(Fraction *)fraction {
    
    Fraction *newFrac = [[Fraction alloc] init];
    newFrac.numerator = self.numerator * fraction.denominator + fraction.numerator * self.denominator;
    newFrac.denominator = self.denominator * fraction.denominator;
    newFrac = [newFrac reduce];
    return newFrac;
    
}

- (Fraction *)minusWithFraction:(Fraction *)fraction {
    
    Fraction *newFrac = [[Fraction alloc] init];
    newFrac.numerator = self.numerator * fraction.denominator - fraction.numerator * self.denominator;
    newFrac.denominator = self.denominator * fraction.denominator;
    newFrac = [newFrac reduce];
    return newFrac;
    
}

- (Fraction *)multiplyWithFraction:(Fraction *)fraction {
    
    Fraction *newFrac = [[Fraction alloc] init];
    newFrac.numerator = self.numerator * fraction.numerator;
    newFrac.denominator = self.denominator * fraction.denominator;
    newFrac = [newFrac reduce];
    return newFrac;
    
}

- (Fraction *)divideWtihFraction:(Fraction *)fraction {
    
    Fraction *newFrac = [[Fraction alloc] init];
    newFrac.numerator = self.numerator * fraction.denominator;
    newFrac.denominator = self.denominator * fraction.numerator;
    newFrac = [newFrac reduce];
    return newFrac;
    
}

- (void)compareWithFraction:(Fraction *)fraction {
    
//    if (self.denominator != fraction.denominator) {
    
        NSInteger nu1 = self.numerator * fraction.denominator - self.denominator * fraction.numerator;
        if (nu1 > 0) {
            NSLog(@"第一个数比第二个数大");
        } else if (nu1 < 0) {
            NSLog(@"第二个数比第一个数大");
        } else {
            NSLog(@"这俩数一样大");
        }
        
//    } else {
//        NSInteger numerator1 = self.numerator - fraction.numerator;
//        if (numerator1 > 0) {
//            NSLog(@"第一个数比第二个数大");
//        } else if (numerator1 < 0) {
//            NSLog(@"第二个数比第一个数大");
//        } else {
//            NSLog(@"这俩数一样大");
//        }
//    }
    
}

- (Fraction *)reduce {
    if (self.denominator == 0) {
        NSLog(@"分母为0,输入有误,无法约分");
        return self;
    } else
    {
    
        NSInteger newNume = self.numerator;
        NSInteger newDeno = self.denominator;
        NSInteger number = 0;
        while (1) {
            number = newDeno;
            newDeno = newNume % newDeno;
            newNume = number;
            if (newDeno == 0) {
                break;
            }
        }
        newNume = self.numerator / number;
        newDeno = self.denominator / number;
        return [[Fraction alloc] initWithNumerator:newNume denominator:newDeno];
    }
}







@end
